<template>
  <div class="dialog">
    <el-dialog :title="titles" :visible="dialogVisible" width="30%" :before-close="handleClose">
      <div>
        <el-form :model="personChildren" :rules="rules" ref="personChildren" label-width="100px" class="demo-ruleForm">
          <el-form-item label="持仓人名字" prop="name">
            <el-input v-model="personChildren.name" :disabled="titles == '修改个人持仓点位'"></el-input>
          </el-form-item>
          <el-form-item label="持仓价格" prop="price"> <el-input v-model.number="personChildren.price"></el-input></el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="updateChildren">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'UpdateDia',
  data() {
    return {
      dialogVisible: false,
      titles: '',
      personGuPiaoFather: {}, //传过来的父级别
      personChildren: {}, //children
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        price: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleClose() {
      this.personGuPiaoFather = {}
      this.personChildren = {}
      this.dialogVisible = false
    },
    open(tilte, item) {
      this.dialogVisible = true
      // console.log(tilte)
      this.titles = tilte
      this.personGuPiaoFather = JSON.parse(JSON.stringify(item))
      if (tilte == '修改个人持仓点位') {
        // console.log(item)
        this.personChildren = {
          name: item.name,
          price: item.price
        }
      }
    },
    updateChildren() {
      this.$refs.personChildren.validate((valid) => {
        if (valid) {
          if (this.titles == '新增个人持仓点位') {
            // console.log(this.personGuPiaoFather)
            this.personChildren.flag = true
            this.personChildren.children = []
            this.personChildren.bond_id = this.personGuPiaoFather.bond_id + '-' + this.personChildren.name
            this.personChildren.increase_rt = (((this.personGuPiaoFather.price - this.personChildren.price) / this.personChildren.price) * 100).toFixed(2) + '%'
            // console.log(this.personChildren)

            this.personGuPiaoFather.children.push(this.personChildren)
            // console.log(this.personGuPiaoFather)
            this.$parent.childrenModify(this.personGuPiaoFather)
            this.handleClose()
          } else {
            let copyFilterGuPiaoList = JSON.parse(window.localStorage.getItem('filterGuPiaoList'))
            // this.personGuPiaoFather.price = this.personChildren.price
            // console.log(this.personGuPiaoFather)
            // console.log(copyFilterGuPiaoList)
            // copyFilterGuPiaoList
            copyFilterGuPiaoList.forEach((item, i) => {
              if (item.children) {
                item.children.forEach((item1, i1) => {
                  if (item1.bond_id == this.personGuPiaoFather.bond_id) {
                    // console.log(item1)
                    // console.log(item)
                    item1.price = this.personChildren.price
                    item1.increase_rt = (((item.price - this.personChildren.price) / this.personChildren.price) * 100).toFixed(2) + '%'
                  }
                })
              }
            })
            // console.log(copyFilterGuPiaoList)
            window.localStorage.setItem('filterGuPiaoList', JSON.stringify(copyFilterGuPiaoList))
            this.$parent.getlist()
            this.handleClose()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped></style>
